import pymysql
import yaml
from base import log


def connect(enviroment, inputdbname):
    logging = log.init(1)
    # 读取application.yml配置
    appcationfile = "application.yml"
    configyml = open(appcationfile, encoding="utf-8")
    config = yaml.load(configyml)
    host = config['mysql'][enviroment]["host"]
    username = config['mysql'][enviroment]["username"]
    password = config['mysql'][enviroment]["password"]
    port = config['mysql'][enviroment]['port']

    dbname = inputdbname
    db = pymysql.connect(
        host=host,
        port=port,
        user=username,
        passwd=password,
        db=dbname,
        charset='utf8')
    return db, db.cursor()
